#! /bin/bash -f
#
# Usage:
#
# Build the event library - build lib
# Build the test suite    - build test
# Clean up all binaries   - build clean
#

build_libevent() 
{
    rm -rf obj
    mkdir  obj

    src="src/*.c"
    for file in $src; do
        #
        # strip the .c extensions
        # strip the file path
        # put the base file in the obj directory
        #
        obj=${file%.*}     
        obj=${obj##*/}  
        obj="obj/"$obj  
            
        #
        # compile the files
        #
        gcc -g -c $file -Iinclude -o $obj
    done
}

build_test()
{
    sh build clean
    sh build lib 
    
    src="test/*.c"
    for file in $src; do  
        #
        # strip the .c extionsions
        # compile the files
        #
        bin=${file%.*}
        gcc -g -o $bin $file -Iinclude obj/*
    done
}

build_clean()
{
    rm -rf obj
    
    bin="test/*"
    for file in $bin; do
        if [ -x "$file" ]; then
            rm -f $file
        fi
    done
}

#
# the default target
#
target='libevent'

if [ $# -lt 1 ]; then
    echo "Building default target: ${target}"
else
    target=$1
fi

if   [ "$target" = "lib" ]; then
    build_libevent
elif [ "$target" = "libevent" ]; then
    build_libevent
elif [ "$target" = "test" ]; then
    build_test
elif [ "$target" = "clean" ]; then 
    build_clean
else   
    echo  "Unknown build target: ${target}"
fi



